#ifndef LOG_4CPP_H
#define LOG_4CPP_H

#include <log4cplus/logger.h>
#include <log4cplus/consoleappender.h>
#include <log4cplus/fileappender.h>
#include <log4cplus/layout.h>
#include <log4cplus/configurator.h>
#include <log4cplus/loggingmacros.h>
#include <log4cplus/initializer.h>

using namespace log4cplus;
using namespace log4cplus::helpers;

// extern log4cplus::Logger logger;

extern log4cplus::Logger logger_trace;
extern log4cplus::Logger logger_debug;
extern log4cplus::Logger logger_info;
extern log4cplus::Logger logger_warn;
extern log4cplus::Logger logger_error;
extern log4cplus::Logger logger_fatal;

#define LOG_TRACE(logEvent) LOG4CPLUS_TRACE(logger_trace, logEvent)
#define LOG_DEBUG(logEvent) LOG4CPLUS_DEBUG(logger_debug, logEvent)
#define LOG_INFO(logEvent) LOG4CPLUS_INFO(logger_info, logEvent)
#define LOG_WARN(logEvent) LOG4CPLUS_WARN(logger_warn, logEvent)
#define LOG_ERROR(logEvent) LOG4CPLUS_ERROR(logger_error, logEvent)
#define LOG_FATAL(logEvent) LOG4CPLUS_FATAL(logger_fatal, logEvent)

class Log4cpp
{
public:
    static Log4cpp *instance();

    // 初始化日志系统
    void initialize(const std::string &configFile = "./config/log.config", unsigned int watchDelayMs = 5000);
    // 检查是否已初始化
    bool isInitialized() const;

private:
    Log4cpp() = default;
    ~Log4cpp() = default;

    Log4cpp(const Log4cpp &) = delete;
    Log4cpp &operator=(const Log4cpp &) = delete;
    Log4cpp(Log4cpp &&) = delete;
    Log4cpp &operator=(Log4cpp &&) = delete;

    class Destroy
    {
    public:
        ~Destroy()
        {
            if (_instance != nullptr)
            {
                delete _instance;
                _instance = nullptr;
            }
        }
    };

public:
    void setConfigurationItems();

private:
    static Log4cpp *_instance;
    static Destroy destroy;

    bool m_initialized = {false};
};

#endif // LOG4CPP_H
